package com.ruoyi.web.controller.chatroom;

import com.ruoyi.chatroom.domain.bo.ChatRecordBo;
import com.ruoyi.chatroom.domain.vo.ChatRecordVo;
import com.ruoyi.chatroom.service.IChatRecordService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.List;

/**
 * 聊天记录Controller
 *
 * @author zhutianyu
 * @date 2021-09-03
 */
@Validated
@Api(value = "聊天记录控制器", tags = {"聊天记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/chatroom/record")
public class ChatRecordController extends BaseController {

    private final IChatRecordService iChatRecordService;

    /**
     * 查询聊天记录列表
     */
    @ApiOperation("查询聊天记录列表")
    @PreAuthorize("@ss.hasPermi('chatroom:record:list')")
    @GetMapping("/list")
    public TableDataInfo<ChatRecordVo> list(@Validated ChatRecordBo bo) {
        return iChatRecordService.queryPageList(bo);
    }

    /**
     * 导出聊天记录列表
     */
    @ApiOperation("导出聊天记录列表")
    @PreAuthorize("@ss.hasPermi('chatroom:record:export')")
    @Log(title = "聊天记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ChatRecordVo> export(@Validated ChatRecordBo bo) {
        List<ChatRecordVo> list = iChatRecordService.queryList(bo);
        ExcelUtil<ChatRecordVo> util = new ExcelUtil<ChatRecordVo>(ChatRecordVo.class);
        return util.exportExcel(list, "聊天记录");
    }

    /**
     * 查询聊天记录
     */
    @ApiOperation("查询聊天记录")
    @PreAuthorize("@ss.hasPermi('chatroom:record:query')")
    @GetMapping()
    public AjaxResult<List<ChatRecordVo>> getInfo(@RequestParam("sendUser") String sendUser, @RequestParam("receiveUser") String receiveUser) {
        return AjaxResult.success(iChatRecordService.queryMessage(sendUser, receiveUser));
    }

    /**
     * 发送消息
     */
    @ApiOperation("发送消息")
    @PreAuthorize("@ss.hasPermi('chatroom:record:add')")
    @Log(title = "聊天记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody ChatRecordBo bo) {
        return toAjax(iChatRecordService.insertByBo(bo));
    }

    /**
     * 修改聊天记录
     */
    @ApiOperation("修改聊天记录")
    @PreAuthorize("@ss.hasPermi('chatroom:record:edit')")
    @Log(title = "聊天记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody ChatRecordBo bo) {
        return toAjax(iChatRecordService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除聊天记录
     */
    @ApiOperation("删除聊天记录")
    @PreAuthorize("@ss.hasPermi('chatroom:record:remove')")
    @Log(title = "聊天记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                   @PathVariable String[] ids) {
        return toAjax(iChatRecordService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 获取当前登录用户ip
     *
     * @return
     */
    @ApiOperation("获取当前登录用户ip")
    @PreAuthorize("@ss.hasPermi('chatroom:record:ip')")
    @GetMapping("/ip")
    public String getIp() {
        HttpServletRequest request = ServletUtils.getRequest();
        final String ip = ServletUtils.getClientIP(request);
        String address = AddressUtils.getRealAddressByIP(ip);
        return address.concat(" | ").concat(ip);
    }
}
